package com.stellar.circle.controller.miniApp.sChatMessage;


import com.stellar.circle.controller.admin.sChatMessage.vo.SChatMessageResponseVO;
import com.stellar.circle.domain.sChatMessage.SChatMessageDomainService;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 私聊消息表控制器
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-06-12 21:21:20
 */
@RequestMapping("/api-miniApp/sChatMessage")
@RestController("miniAppSChatMessageController")
@Slf4j
public class SChatMessageController {

    @Resource
    private SChatMessageDomainService sChatMessageDomainService;

    /**
     * 删除/撤回私聊 - 前端手动判断，3分钟后不能撤回
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(sChatMessageDomainService.delete(id));
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/markAsRead")
    public Result<Boolean> markAsRead(@RequestParam List<Long> messageId) {
        return Result.success(sChatMessageDomainService.markAsRead(messageId));
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/getUnreadCount")
    public Result<Integer> getUnreadCount() {
        return Result.success(sChatMessageDomainService.getUnreadCount());
    }

    /**
     * 获取聊天页面消息列表
     */
    @GetMapping("/getMessageList")
    public Result<PageResult<SChatMessageResponseVO>> getMessageList(@RequestParam int page,
                                                                     @RequestParam int pageSize) {
        return Result.success(sChatMessageDomainService.getMessageList(page, pageSize));
    }

    /**
     * 获取用户对话历史消息
     *
     * @param userId    用户ID
     * @param friendId  好友ID
     * @param lastMsgId 最后一条消息ID（用于分页）
     * @return 消息列表
     */
    @GetMapping("/history")
    public Result<List<SChatMessageResponseVO>> getHistory(@RequestParam Long userId,
                                                           @RequestParam Long friendId,
                                                           @RequestParam(defaultValue = "0") Long lastMsgId) {
        String conversationId = sChatMessageDomainService.generateConversationId(userId, friendId);
        List<SChatMessageResponseVO> history = sChatMessageDomainService.getHistory(conversationId, lastMsgId, 20);
        return Result.success(history);
    }


}
